#include <event2/event.h>
#include <event2/listener.h>
#include <event2/thread.h>
#include <string.h>
#include <iostream>
#ifndef _WIN32
#include <signal.h>
#endif
using namespace std;

#define PORT 8080

void listen_cb(struct evconnlistener *ev, evutil_socket_t sfd, 
                                struct sockaddr *addr, int socklen, void *arg){
        cout<<"listen_cb"<<endl;
}

int main(int argc, char const *argv[]){
#ifdef _WIN32
        //初始化socket库
        WSADATA wsa;
        WSAStartup(MAKEWORD(2,2), &wsa);
#else
        //忽略管道信号 发送数据给已关闭的socket
        if(signal(SIGPIPE,SIG_IGN) == SIG_IGN){
                return 1;
        }
#endif

        //创建libevent的上下文
        //创建配置上下文
        event_config *conf = event_config_new();

        //显示支持的网络模式
        const char **methods = event_get_supported_methods();
        cout<<"supported methods:"<<endl;
        for (int i = 0; methods[i] != NULL; ++i){
                cout<<"\t"<<methods[i]<<endl;
        }

        //设置特征
        //1 设置了EV_FEATURE_FDS 其他特征就无效了 网络模型不支持epoll
        //event_config_require_features(conf, EV_FEATURE_FDS);

        //2.设置网络模型使用select
        //event_config_avoid_method(conf, "epoll"); //去掉epoll
        //event_config_avoid_method(conf, "poll");//去掉poll

        //3.windows中支持IOCP
#ifdef _WIN32
        event_config_set_flag(conf, EVENT_BASE_FLAG_STARTUP_IOCP);
        //设置iocp的线程
        evthread_use_windows_threads();
        //设置cpu数量
        SYSTEM_INFO si;
        GetSystemInfo(&si);
        event_config_set_num_cpus_hint(conf, si.dwNumberOfProcessors);
#endif

        event_base *base = event_base_new_with_config(conf);
        event_config_free(conf);

        if(!base){
                cerr<<"event_base_new_with_config failed"<<endl;
                base = event_base_new();
                if(!base){
                        cerr<<"event_base failed!"<<endl;
                        return 0;
                }
        }        
        
        cout<<"event_base_new_with_config success"<<endl;

        //获取当前网络模型
        cout<<"current method is "<<event_base_get_method(base)<<endl;

        //获取当前的base的特征
        int features = event_base_get_features(base);
        if(features & EV_FEATURE_ET)
                cout<<"EV_FEATURE_ET ok"<<endl;
        else
                cout<<"EV_FEATURE_ET no"<<endl;

        if(features & EV_FEATURE_O1)
                cout<<"EV_FEATURE_O1 ok"<<endl;
        else
                cout<<"EV_FEATURE_O1 no"<<endl;
        
        if(features & EV_FEATURE_FDS)
                cout<<"EV_FEATURE_FDS ok"<<endl;
        else
                cout<<"EV_FEATURE_FDS no"<<endl;
        
        if(features & EV_FEATURE_EARLY_CLOSE)
                cout<<"EV_FEATURE_EARLY_CLOSE ok"<<endl;
        else
                cout<<"EV_FEATURE_EARLY_CLOSE no"<<endl;

        sockaddr_in sin;
        memset(&sin, 0, sizeof(sin));
        sin.sin_family = AF_INET;
        sin.sin_port = htons(PORT);
        
        evconnlistener *ev = evconnlistener_new_bind(base, listen_cb,base,
                LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE_PORT, 
                -1, (sockaddr *)&sin, sizeof(sin));

        event_base_dispatch(base);

        if(ev)
                evconnlistener_free(ev);

        if(base)
                event_base_free(base);
#ifdef _WIN32
        WSACleanup();
#endif
        return 0;
}